/*
 * @Author: lerio.cheng
 * @Date:   2017-02-22 16:44:17
 * @Last Modified by:   DELL
 * @Last Modified time: 2017-02-24 12:49:10
 */

'use strict';

app.controller('addHouseCtrl', function($scope, tipService, $window, $http, rest, $state, ngDialog, user, $location,uploadConf,uploadConf2,mapSev,uiLoad,$rootScope) {
    $scope.params = {
        landlordId: user.get('landlordId'),
        BedRoom: 1,
        LiveRoom: 1,
        KitchenRoom: 1,
        ToiletRoom: 1
    };

    $scope.params2 = {};
    if ($scope.params.CountyId != null || $scope.params.CountyId != undefined) {
        var cityName1 = $scope.params.CountyId;
    }else{
        var cityName1 = ""
    }
    
    console.info(cityName1)
	

    $scope.disable_save_btn = false;

    $scope.column2 = 0;      //分栏
    $scope.maxImgNum = 100;    //最大图片数量
    $scope.imgNew = [
        {
            text:'卧室图片',
            name: 'bedRoom',
            value: [],
            picType: 0,
            hink: '至少两张'
        },
        {
            text:'客厅图片',
            name: 'livingRoom',
            value: [],
            picType: 1,
            hink: '至少两张'
        },
        {
            text:'卫生间图片',
            name: 'bathRoom',
            value: [],
            picType: 2,
            hink: '至少两张'
        },
        {
            text:'厨房图片',
            name: 'kitchenRoom',
            value: [],
            picType: 3,
            hink: '至少两张'
        },
        {
            text:'周边环境',
            name: 'environment',
            value: [],
            picType: 4,
            hink: '至少两张'
        },
        {
            text:'房门照片',
            name: 'door',
            value: [],
            picType: 10,
            hink: '至少两张'
        }
    ];

    uploadConf.column2 = $scope.column2;
    uploadConf.maxImgNum =  $scope.maxImgNum;
    uploadConf.imgNew = $scope.imgNew;


    // 户型
    $scope.houseType = { selected: {name: '一室户', value: 7} };
    $scope.houseTypeList = [{
        name: '一室户',
        value: 7
    }, {
        name: '一室一厅',
        value: 0
    }, {
        name: '一室半',
        value: 1
    }, {
        name: '二室一厅',
        value: 2
    }, {
        name: '二室半',
        value: 3
    }, {
        name: '三室一厅',
        value: 4
    }, {
        name: '四室以上',
        value: 5
    }, {
        name: '复式',
        value: 6
    }];
    // 朝向4013
    $scope.orientation = {};
    $scope.orientationList = [{
        name: '东',
        value: 0
    }, {
        name: '南',
        value: 1
    }, {
        name: '西',
        value: 2
    }, {
        name: '北',
        value: 3
    }, {
        name: '东南',
        value: 4
    }, {
        name: '西南',
        value: 5
    }, {
        name: '东北',
        value: 6
    }, {
        name: '西北',
        value: 7
    }, {
        name: '南北',
        value: 8
    }];
    // 装修情况
    $scope.renovation = { selected: {name: '简装', value: 1} };
    $scope.renovationList = [{
        name: '毛坯',
        value: 0
    }, {
        name: '简装',
        value: 1
    }, {
        name: '中等装修',
        value: 2
    }, {
        name: '精装修',
        value: 3
    }, {
        name: '豪华装修',
        value: 4
    }];
    // 支付方式
    $scope.payType = {};
    $scope.payTypeList = [{
        name: '一月一付',
        value: 0
    }, {
        name: '二月一付',
        value: 1
    }, {
        name: '三月一付',
        value: 2
    }];

    // 押金
    $scope.deposit = {};
    $scope.depositList = [{
        name: '无押金',
        value: 0
    }, {
        name: '一月押金',
        value: 1
    }, {
        name: '两月押金',
        value: 2
    }, {
        name: '三月押金',
        value: 3
    }];
    // 出租类型
    $scope.leaseType = { selected: {name: '整租', value: 0} };
    $scope.leaseTypeList = [{
        name: '整租',
        value: 0
    }, {
        name: '合租',
        value: 1
    }];
    // 是否已出租
    $scope.isRent = { selected: {name: '未出租', value: 0} };
    $scope.isRentList = [{
            name: '已出租',
            value: 1
        }, {
            name: '未出租',
            value: 0
        }]
        // 房屋设施
    $scope.houseDevice = {};
    $scope.houseDeviceList = [{
        name: '床',
        value: 'Bed'
    }, {
        name: '沙发',
        value: 'Sofa'
    }, {
        name: '餐桌',
        value: 'Board'
    }, {
        name: '书桌',
        value: 'Desk'
    }, {
        name: '椅子',
        value: 'Chair'
    }, {
        name: '洗衣机',
        value: 'WashMachine'
    }, {
        name: '冰箱',
        value: 'Refrigerator'
    }, {
        name: '电视',
        value: 'Television'
    }, {
        name: '空调',
        value: 'AirCondition'
    }, {
        name: '热水器',
        value: 'WaterHeater'
    }, {
        name: '油烟机',
        value: 'LampblackMachine'
    }, {
        name: '橱柜',
        value: 'Ambry'
    }, {
        name: '无线网',
        value: 'Wifi'
    }, {
        name: '燃气灶',
        value: 'Stove'
    }, {
        name: '微波炉',
        value: 'MicrowaveOven'
    }];

    // // 卧室照片
    // $scope.params.HousePic = []
    // $scope.bedRoom = $scope.bedRoom || [];

    // $scope.$on('bedRoomChange', function(d, data) {
    //     deletSame(0);
    //     for (var i in data.list) {
    //         $scope.params.HousePic.push({ picPath: data.list[i], picType: 0, sort: 0 });
    //     }
    // });

    // // 客厅照片
    // $scope.livingRoom = $scope.livingRoom || [];
    // $scope.$on('livingRoomChange', function(d, data) {
    //     deletSame(1);
    //     for (var i in data.list) {
    //         $scope.params.HousePic.push({ picPath: data.list[i], picType: 1, sort: 0 });
    //     }
    // });
    // // 卫生间照片
    // $scope.bathRoom = $scope.bathRoom || [];
    // $scope.$on('bathRoomChange', function(d, data) {
    //     deletSame(2);
    //     for (var i in data.list) {
    //         $scope.params.HousePic.push({ picPath: data.list[i], picType: 2, sort: 0 });
    //     }
    // });

    // // 厨房照片
    // $scope.kitchenRoom = $scope.kitchenRoom || [];
    // $scope.$on('kitchenRoomChange', function(d, data) {
    //     deletSame(3);
    //     for (var i in data.list) {
    //         $scope.params.HousePic.push({ picPath: data.list[i], picType: 3, sort: 0 });
    //     }
    // });

    // // 外部照片
    // $scope.environment = $scope.environment || [];
    // $scope.$on('environmentChange', function(d, data) {
    //     deletSame(4);
    //     for (var i in data.list) {
    //         $scope.params.HousePic.push({ picPath: data.list[i], picType: 4, sort: 0 });
    //     }
    // });
    // // 房门照片
    // $scope.door = $scope.door || [];
    // $scope.$on('doorChange', function(d, data) {
    //     deletSame(5);
    //     for (var i in data.list) {
    //         $scope.params.HousePic.push({ picPath: data.list[i], picType: 10, sort: 0 });
    //     }
    // });

    $scope.picStorage = function(){
        ngDialog.openConfirm({
          template: 'js/module/decorate/picStorage.html',
          resolve: {
            load: function() {
              return uiLoad.load(['js/module/decorate/picStorage.js'])
            }
          },
          data:{
            // decorateId:decorateId
          },
          scope: $scope,
          width: 800,
          // height: 700
        }).then(function(data) {

        }, function(reason) {

        });
    };


    // 监听省市县
    $scope.$watch('areaid.selected', function(newValue, oldValue) {
        newValue ? $scope.params.CountyId = newValue.id : "";
        newValue ? cityName1 = newValue.name : "";
        
    });
    $("#suggestId").blur(function(){
        $scope.params.AreaName = document.getElementById("suggestId").value;
    })

    $scope.$watch('params.HouseSize', function(newValue, oldValue) {
         if (newValue < 1){
                $scope.params.HouseSize =1;
        }
    });
    $scope.$watch('params.HouseRent', function(newValue, oldValue) {
         if (newValue < 1){
                $scope.params.HouseRent =1;
        }
    });
    $scope.$watch('params.CurrFloor', function(newValue, oldValue) {
         if (newValue < 1){
                $scope.params.CurrFloor =1;
        }
    });
    $scope.bed = true;
    $scope.sofa = true;
    $scope.board = true;
    $scope.desk = true;
    $scope.chair = true;
    $scope.washMachine = true;
    $scope.refrigerator = true;
    $scope.television = true;
    $scope.airCondition = true;
    $scope.waterHeater = true;
    $scope.lampblackMachine = true;
    $scope.ambry = true;
    $scope.wifi = true;
    $scope.stove = true;
    $scope.microwaveOven = true;

    $scope.$watch('bed',function(n,o){
        $scope.params.bed = n ? 1 : 0;
    });
    $scope.$watch('sofa',function(n,o){
        $scope.params.sofa = n ? 1 : 0;
    });
    $scope.$watch('board',function(n,o){
        $scope.params.board = n ? 1 : 0;
    });
    $scope.$watch('desk',function(n,o){
        $scope.params.desk = n ? 1 : 0;
    });
    $scope.$watch('chair',function(n,o){
        $scope.params.chair = n ? 1 : 0;
    });
    $scope.$watch('washMachine',function(n,o){
        $scope.params.washMachine = n ? 1 : 0;
    });
    $scope.$watch('refrigerator',function(n,o){
        $scope.params.refrigerator = n ? 1 : 0;
    });
    $scope.$watch('television',function(n,o){
        $scope.params.television = n ? 1 : 0;
    });
    $scope.$watch('airCondition',function(n,o){
        $scope.params.airCondition = n ? 1 : 0;
    });
    $scope.$watch('waterHeater',function(n,o){
        $scope.params.waterHeater = n ? 1 : 0;
    });
    $scope.$watch('lampblackMachine',function(n,o){
        $scope.params.lampblackMachine = n ? 1 : 0;
    });
    $scope.$watch('ambry',function(n,o){
        $scope.params.ambry = n ? 1 : 0;
    });
    $scope.$watch('wifi',function(n,o){
        $scope.params.wifi = n ? 1 : 0;
    });
    $scope.$watch('stove',function(n,o){
        $scope.params.stove = n ? 1 : 0;
    });
    $scope.$watch('microwaveOven',function(n,o){
        $scope.params.microwaveOven = n ? 1 : 0;
    });
    $scope.chooseAll = function(n,o){
        if ($scope.params.bed == 1) {
            $scope.params.bed = 0;
            $scope.bed = false;
        }else if($scope.params.bed == 0) {
            $scope.params.bed = 1;
            $scope.bed = true;
        }
        if ($scope.params.sofa == 1) {
            $scope.params.sofa = 0;
            $scope.sofa = false;
        }else if($scope.params.sofa == 0) {
            $scope.params.sofa = 1;
            $scope.sofa = true;
        }
        if ($scope.params.board == 1) {
            $scope.params.board = 0;
            $scope.board = false;
        }else if($scope.params.board == 0) {
            $scope.params.board = 1;
            $scope.board = true;
        }
        if ($scope.params.desk == 1) {
            $scope.params.desk = 0;
            $scope.desk = false;
        }else if($scope.params.desk == 0) {
            $scope.params.desk = 1;
            $scope.desk = true;
        }
        if ($scope.params.chair == 1) {
            $scope.params.chair = 0;
            $scope.chair = false;
        }else if($scope.params.chair == 0) {
            $scope.params.chair = 1;
            $scope.chair = true;
        }
        if ($scope.params.washMachine == 1) {
            $scope.params.washMachine = 0;
            $scope.washMachine = false;
        }else if($scope.params.washMachine == 0) {
            $scope.params.washMachine = 1;
            $scope.washMachine = true;
        }
        if ($scope.params.refrigerator == 1) {
            $scope.params.refrigerator = 0;
            $scope.refrigerator = false;
        }else if($scope.params.refrigerator == 0) {
            $scope.params.refrigerator = 1;
            $scope.refrigerator = true;
        }
        if ($scope.params.television == 1) {
            $scope.params.television = 0;
            $scope.television = false;
        }else if($scope.params.television == 0) {
            $scope.params.television = 1;
            $scope.television = true;
        }
        if ($scope.params.airCondition == 1) {
            $scope.params.airCondition = 0;
            $scope.airCondition = false;
        }else if($scope.params.airCondition == 0) {
            $scope.params.airCondition = 1;
            $scope.airCondition = true;
        }
        if ($scope.params.waterHeater == 1) {
            $scope.params.waterHeater = 0;
            $scope.waterHeater = false;
        }else if($scope.params.waterHeater == 0) {
            $scope.params.waterHeater = 1;
            $scope.waterHeater = true;
        }
        if ($scope.params.lampblackMachine == 1) {
            $scope.params.lampblackMachine = 0;
            $scope.lampblackMachine = false;
        }else if($scope.params.lampblackMachine == 0) {
            $scope.params.lampblackMachine = 1;
            $scope.lampblackMachine = true;
        }
        if ($scope.params.ambry == 1) {
            $scope.params.ambry = 0;
            $scope.ambry = false;
        }else if($scope.params.ambry == 0) {
            $scope.params.ambry = 1;
            $scope.ambry = true;
        }
        if ($scope.params.wifi == 1) {
            $scope.params.wifi = 0;
            $scope.wifi = false;
        }else if($scope.params.wifi == 0) {
            $scope.params.wifi = 1;
            $scope.wifi = true;
        }
        if ($scope.params.stove == 1) {
            $scope.params.stove = 0;
            $scope.stove = false;
        }else if($scope.params.stove == 0) {
            $scope.params.stove = 1;
            $scope.stove = true;
        }
        if ($scope.params.microwaveOven == 1) {
            $scope.params.microwaveOven = 0;
            $scope.microwaveOven = false;
        }else if($scope.params.microwaveOven == 0) {
            $scope.params.microwaveOven = 1;
            $scope.microwaveOven = true;
        }
    }

    //获取房源模板列表
    var param_3012 = {};
    param_3012.landlordId = $scope.params.landlordId;
    rest.post({
        methodId: '3012',
        data: param_3012,
        success: function(res) {
            if (res.code == 0) {
                $scope.$apply(function(){
                    $scope.picModel = { selected: {name: '请选择模板', value: true} };
                    $scope.picModelList = res.res.data;
                });
            } else {

            }
            $scope.disable_save_btn = false;
        },
        error: function() {
            $scope.disable_save_btn = false;
        }
    });

    //获取房源图片
    $scope.chooseModel = function(index,picModelList) {
        $scope.editPicFlag = true;

        $scope.imgNew[0].value = [];
        $scope.imgNew[1].value = [];
        $scope.imgNew[2].value = [];
        $scope.imgNew[3].value = [];
        $scope.imgNew[4].value = [];
        $scope.imgNew[5].value = [];

        var picModelId = picModelList[index].id;
        // console.log( picModelId );

        var param_3013 = {};
        param_3013.id = picModelId;
        $rootScope.modelId = picModelId;

        rest.post({
            methodId: '3013',
            data: param_3013,
            success: function(res) {
                if (res.code == 0) {
                    var data = res.res.data.content;
                    data = JSON.parse( data );
                    for (var i in data) {
                        if (data[i].PicType == 0) {
                            $scope.imgNew[0].value.push(data[i].PicPath);
                        } else if (data[i].PicType == 1) {
                            $scope.imgNew[1].value.push(data[i].PicPath);
                        } else if (data[i].PicType == 2) {
                            $scope.imgNew[2].value.push(data[i].PicPath);
                        } else if (data[i].PicType == 3) {
                            $scope.imgNew[3].value.push(data[i].PicPath);
                        } else if (data[i].PicType == 4) {
                            $scope.imgNew[4].value.push(data[i].PicPath);
                        } else if (data[i].PicType == 10) {
                            $scope.imgNew[5].value.push(data[i].PicPath);
                        }
                    }
                }
                $scope.disable_save_btn = false;
                $scope.$apply();
            },
            error: function() {
                $scope.disable_save_btn = false;
            }
        });
        $scope.$apply();
    }

    //获取省份列表
    var param_1055 = {};
    param_1055.parentId = 0;
    rest.post({
        methodId: '1055',
        data: param_1055,
        success: function(res) {
            if (res.code == 0) {
                $scope.$apply(function(){
                    $scope.provinceid = { selected: {name: '请选择省份...', value: true} };
                    $scope.cityid = { selected: {name: '请选择城市...', value: true} };
                    $scope.areaid = { selected: {name: '请选择区域...', value: true} };
                    $scope.streetid = { selected: {name: '请选择街道...', value: true} };
                });
                $scope.provinceList = res.res.data;

            } else {

            }
            $scope.disable_save_btn = false;
        },
        error: function() {
            $scope.disable_save_btn = false;
        }
    });

    $scope.chooseProvince = function(index,provinceList){
        console.log(provinceList[index]);
        $scope.params.ProvinceId = provinceList[index].id;
        var provincelId = provinceList[index].id;
        var paramCity = {};
        paramCity.parentId = provincelId;
        rest.post({
            methodId: '1055',
            data: paramCity,
            success: function(res) {
                if (res.code == 0) {
                    
                    $scope.cityList = res.res.data;
                } else {

                }
                $scope.disable_save_btn = false;
            },
            error: function() {
                $scope.disable_save_btn = false;
            }
        });
    }

    $scope.chooseCity = function(index,cityList){
        console.log(cityList[index]);
        cityName1 = cityList[index].name;
        $scope.params.CityId = cityList[index].id;
        var cityId = cityList[index].id;
        var paramArea = {};
        paramArea.parentId = cityId;
        rest.post({
            methodId: '1055',
            data: paramArea,
            success: function(res) {
                if (res.code == 0) {
                    
                    $scope.areaList = res.res.data;
                } else {

                }
                $scope.disable_save_btn = false;
            },
            error: function() {
                $scope.disable_save_btn = false;
            }
        });

        //地址智能搜索补全
        $scope.ca = new BMap.Autocomplete({"input" : "suggestId","location" : cityName1});//建立一个自动完成的对象fullAddress
        $scope.ca.addEventListener("onconfirm", function(e) { //鼠标点击下拉列表后的事件
            var areaname = e.item.value.business;
            document.getElementById('suggestId').value = areaname;
            if (!$scope.provinceid.selected || !$scope.cityid.selected || !$scope.areaid.selected) {
                    tipService.show("body", {
                        message: "所在城市地址必须填全",
                        notice: "danger"
                    }, 3);
                    document.getElementById('suggestId').value = '';
                    return;
                }else{
                    mapSev($scope.provinceName,$scope.cityName,$scope.districtName,areaname,'fullAddress');
                }

        });
    }
    $scope.chooseArea = function(index,areaList){
        console.log(areaList[index]);
        $scope.params.CountyId = areaList[index].id;
        var areaId = areaList[index].id;
        var paramStreet = {};
        paramStreet.parentId = areaId;
        rest.post({
            methodId: '1055',
            data: paramStreet,
            success: function(res) {
                if (res.code == 0) {
                    
                    $scope.streetList = res.res.data;
                } else {

                }
                $scope.disable_save_btn = false;
            },
            error: function() {
                $scope.disable_save_btn = false;
            }
        });
    }

    $scope.chooseStreet = function(index,streetList){
        $scope.params.StreetId = streetList[index].id;
        $scope.params.StreetName = streetList[index].name;
    }

    $scope.$on('contractDate.start', function(event, data) {
      $scope.params2.contractStartDate = data + " 00:00:00";
    })
    $scope.$on('contractDate.end', function(event, data) {
      $scope.params2.contractEndDate = data + " 00:00:00";
    })

    $scope.$on('contractPics', function(event, data) {
      $scope.params2.contractPics = data.value;
      console.log(data.value);
    })
    $scope.$on('roadPics', function(event, data) {
      $scope.params2.roadPics = data.value;
    })
    $scope.$on('doorplatePics', function(event, data) {
      $scope.params2.doorplatePics = data.value;
    })
    $scope.$on('doubleDoorPics', function(event, data) {
      $scope.params2.doubleDoorPics = data.value;
    })
    $scope.$on('houseCardPics', function(event, data) {
      $scope.params2.houseCardPics = data.value;
    })
    $scope.$on('doorPhotoPics', function(event, data) {
      $scope.params2.doorPhotoPics = data.value;
    })

    $scope.params2.landlordId = user.get('landlordId');

    $scope.$watch('params2.expectAmount', function(newValue, oldValue) {               
        if (newValue < 1){
            $scope.params2.expectAmount = 1;
        }else{
            $scope.params2.expectAmount = newValue;
        }
    });
    $scope.selectApply = true;
    $scope.$watch('selectApply',function(n,o){
        if ($scope.selectApply == 1) {
            // console.log("申请");
            $scope.showApplyForm = 1;
        }else{
            // console.log("不申请");
            $scope.showApplyForm = 0;           
        }
    });


    function getNowFormatDate() {
        var date = new Date();
        var seperator1 = "/";
        var month = date.getMonth() + 1;
        var strDate = date.getDate();
        if (month >= 1 && month <= 9) {
            month = "0" + month;
        }
        if (strDate >= 0 && strDate <= 9) {
            strDate = "0" + strDate;
        }
        $scope.currentdate = date.getFullYear() + seperator1 + month + seperator1 + strDate
                + " ";
    }
    
    getNowFormatDate();

    $("#contractStartDate1").blur(function(){

        var patt1 = new RegExp(/^(?:(?!0000)[0-9]{4}([-/.]?)(?:(?:0?[1-9]|1[0-2])\1(?:0?[1-9]|1[0-9]|2[0-8])|(?:0?[13-9]|1[0-2])\1(?:29|30)|(?:0?[13578]|1[02])\1(?:31))|(?:[0-9]{2}(?:0[48]|[2468][048]|[13579][26])|(?:0[48]|[2468][048]|[13579][26])00)([-/.]?)0?2\2(?:29))$/);
        if ($scope.params2.contractStartDate != undefined) {

            $scope.params2.contractStartDate = $scope.params2.contractStartDate.replace(/\./g, '/');
            if ($scope.params2.contractStartDate.substr(0,2) != 20) {
                console.log($scope.params2.contractStartDate)
                $scope.params2.contractStartDate = "20" +  $scope.params2.contractStartDate;
            }
            var result = patt1.test($scope.params2.contractStartDate);
            if (result == true) {
                $scope.dateError1 = 0;
                $scope.params2.contractStartDate = moment($scope.params2.contractStartDate).format('YYYY/MM/DD');
                $scope.params2.contractEndDate = moment($scope.params2.contractStartDate).add(2, 'years').subtract(1, 'days').format('YYYY/MM/DD');
            }else if(result==false){
                $scope.params2.contractEndDate = "";
                $scope.dateError1 = 1;
            }
        }else{
            $scope.params2.contractEndDate = "";
        }
        $scope.$apply();
    });

    var flag = true;
    if (flag) {
        $scope.saveData = function() {
            $scope.params.Address = angular.element('#fullAddress').val();
            flag = false;
            if (!$scope.params.ProvinceId || !$scope.params.CityId || !$scope.params.CountyId || !$scope.params.StreetId) {
                tipService.show("body", {
                    message: "所在省市区街道地址必须填全",
                    notice: "danger"
                }, 3);
                return;
            }
            if (!$scope.orientation.selected || !$scope.params.HouseSize) {
                tipService.show("body", {
                    message: "朝向、面积均不能为空",
                    notice: "danger"
                }, 3);
                return;
            }
            if (!$scope.renovation.selected) {
                tipService.show("body", {
                    message: "装修情况不能为空",
                    notice: "danger"
                }, 3);
                return;
            }

            if (!$scope.params.CurrFloor || !$scope.params.Floors || $scope.params.CurrFloor<=0 || $scope.params.Floors<=0 || $scope.params.Floors<$scope.params.CurrFloor) {
                tipService.show("body", {
                    message: "层高、总楼层数格式不正确",
                    notice: "danger"
                }, 3);
                return;
            }
            if (!$scope.params.roomCode || !$scope.params.AreaName || !$scope.params.Address || !$scope.params.buildingNo || !$scope.params.unit) {
                tipService.show("body", {
                    message: "详细地址未填全",
                    notice: "danger"
                }, 3);
                return;
            }
            if (!$scope.leaseType.selected || !$scope.params.HouseRent || !$scope.isRent.selected || !$scope.payType.selected || !$scope.deposit.selected) {
                tipService.show("body", {
                    message: "出租信息栏信息未填全",
                    notice: "danger"
                }, 3);
                return;
            };
            for (var i in $scope.imgNew) {
               if ($scope.imgNew[i].value.length < 2) {
                tipService.show("body", {
                    message: "每项至少上传两张照片",
                    notice: "danger"
                }, 3);
                return;
              }
            }

            if($scope.selectApply == 1){

                if ($scope.params2.expectAmount == undefined || $scope.params2.expectAmount <= 0) {
                    $scope.disable_save_btn = false;
                    tipService.show("body", {
                        message: "装修申请额度不能小于等于0",
                        notice:"danger"
                    }, 3);
                    return false;
                }
                if ($scope.params2.contractPics == undefined || $scope.params2.contractPics.length <= 1) {
                    $scope.disable_save_btn = false;
                    tipService.show("body", {
                        message: "合同照片至少上传两张",
                        notice:"danger"
                    }, 3);
                    return false;
                }
                if ($scope.params2.roadPics ==undefined || $scope.params2.roadPics.length <= 1) {
                    $scope.disable_save_btn = false;
                    tipService.show("body", {
                        message: "路牌照片至少上传两张",
                        notice:"danger"
                    }, 3);
                    return false;
                }
                if ($scope.params2.doorplatePics == undefined || $scope.params2.doorplatePics.length <= 1) {
                    $scope.disable_save_btn = false;
                    tipService.show("body", {
                        message: "门牌照片至少上传两张",
                        notice:"danger"
                    }, 3);
                    return false;
                }
                if ($scope.params2.doubleDoorPics == undefined || $scope.params2.doubleDoorPics.length <= 1) {
                    $scope.disable_save_btn = false;
                    tipService.show("body", {
                        message: "半开门照片至少上传两张",
                        notice:"danger"
                    }, 3);
                    return false;
                }
                if ($scope.params2.houseCardPics == undefined || $scope.params2.houseCardPics.length <= 1) {
                    $scope.disable_save_btn = false;
                    tipService.show("body", {
                        message: "房产证照片至少上传两张",
                        notice:"danger"
                    }, 3);
                    return false;
                }
                if ($scope.params2.doorPhotoPics == undefined || $scope.params2.doorPhotoPics.length <= 1) {
                    $scope.disable_save_btn = false;
                    tipService.show("body", {
                        message: "小区门口照片至少上传两张",
                        notice:"danger"
                    }, 3);
                    return false;
                }
                if ($scope.params2.contractStartDate == undefined || $scope.params2.contractEndDate == undefined) {
                    $scope.disable_save_btn = false;
                    tipService.show("body", {
                        message: "装修评估包租日期未填写完全",
                        notice:"danger"
                    }, 3);
                    return;
                }
                if ($scope.params2.contractStartDate > $scope.params2.contractEndDate) {
                    $scope.disable_save_btn = false;
                    tipService.show("body", {
                        message: "结束日期不能早于起始日期",
                        notice:"danger"
                    }, 3);
                    return;
                }
            }


            $scope.disable_save_btn = true;
            $scope.params.orientation = $scope.orientation.selected.value;
            $scope.params.renovation = $scope.renovation.selected.value;
            $scope.params.payType = $scope.payType.selected.value;
            $scope.params.deposit = $scope.deposit.selected.value;
            $scope.params.leaseType = $scope.leaseType.selected.value;
            $scope.params.isRent = $scope.isRent.selected.value;
            $scope.params.houseType = $scope.houseType.selected.value;
            $scope.params.IsAnyTime = 1; // 随时可入住
            
            //照片上传
            $scope.params.HousePic = [];
            for (var i in uploadConf.imgNew) {
                for (var j in uploadConf.imgNew[i].value) {
                    $scope.params.HousePic.push({picPath: uploadConf.imgNew[i].value[j],picType: uploadConf.imgNew[i].picType, sort: 0});
                }
            }

            console.log($scope.params2);

            for (var i in $scope.houseDevice.selected) {
                $scope.params[$scope.houseDevice.selected[i].value] = 1;
            }

            rest.post({
                methodId: '4007',
                data: $scope.params,
                success: function(res) {
                    $scope.params2.houseId = res.res.data.houseId;
                    if (res.code == 0) {
                        tipService.show("body", {
                            message: "房源数据提交成功,跳转中......"
                        }, 3);

                        if($scope.selectApply == 1){
                            rest.post({
                                methodId: '4009',
                                data: $scope.params2,
                                success: function(res) {
                                  // $state.go('app.houseOwner.list');
                                  if (res.code == 0) {
                                    $location.path('/app/houseOwner/list');
                                    console.log($scope.params2.houseId);
                                  }else{
                                    tipService.show("body", {
                                        message: res.res.msg,
                                        notice: "danger"
                                    }, 3);
                                    $scope.disable_save_btn = false;
                                  }
                                  flag = true;
                                },
                                error: function() {
                                    $scope.disable_save_btn = false;
                                }
                            });
                        }
                        $location.path('/app/houseOwner/list');
                        flag = true;
                    }else{
                        tipService.show("body", {
                            message: res.res.msg,
                            notice: "danger"
                        }, 3);
                    }
                    $scope.disable_save_btn = false;
                },
                error: function() {
                    $scope.disable_save_btn = false;
                }
            })

        }
    }

    $scope.backList = function() {
        $scope.params = {};
        $location.path('/app/houseOwner/list');
    }

    $scope.addModelPics = function () {
        $state.go('app.houseOwner.housePicsTemplet');
    }

    $scope.editModelPics = function(templetId){
        $window.localStorage.setItem('templetId',JSON.stringify({id:$rootScope.modelId}));
        $state.go('app.houseOwner.editHousePicsTemplet',{id: $rootScope.modelId});
        // console.log($rootScope.modelId);
    }

})

// app.controller('ContractTimeCtrl',function($scope){

//   $scope.today = function() {
//       $scope.contractStartDate = moment().format('YYYY/MM/DD');
//       $scope.contractEndDate = moment().add(2,'years').subtract(1, 'days').format('YYYY/MM/DD');
//     };

//     $scope.closeAll = function() {
//       $scope.openContractBeginDated =
//       $scope.openContractEndDated = false;
//     }

//     $scope.openContractBeginDate = function($event) {
//       $event.preventDefault();$event.stopPropagation();$scope.closeAll();
//       $scope.openContractBeginDated = true;
//     };
//     $scope.openContractEndDate = function($event) {
//       $event.preventDefault();$event.stopPropagation();$scope.closeAll();
//       $scope.openContractEndDated = true;
//     };

//     $scope.dateOptions = {
//       format: 'yyyy/MM/dd',
//       startingDay: 1,
//       class: 'datepicker'
//     };

//     $scope.format = 'yyyy/MM/dd';

//     $scope.today();

//     $scope.$watch('contractStartDate',function(newValue,oldValue){
//       if("[object Date]" === Object.prototype.toString.call(newValue)) {
//          $scope.$emit('contractDate.start',moment(newValue).format('YYYY/MM/DD'))
//       }
//     });
//     $scope.$watch('contractEndDate',function(newValue,oldValue){
//       if("[object Date]" === Object.prototype.toString.call(newValue)) {
//          $scope.$emit('contractDate.end',moment(newValue).format('YYYY/MM/DD'))
//       }
//     });
// });

app.controller('applyCtrl', function($scope, tipService, $window, $http, rest, $state, ngDialog, user, $location,uploadConf,uploadConf2,mapSev,uiLoad,$rootScope) {
    
    $scope.params2 = {};

    $scope.column2 = 0;      //分栏
    $scope.maxImgNum = 10;    //最大图片数量
    $scope.imgNew2 = [
        {
            text:'合同图片集合',
            name: 'contractPics',
            value: [],
            picType: 11,
            hink: '至少两张'
        },
        {
            text:'路牌照片',
            name: 'roadPics',
            value: [],
            picType: 12,
            hink: '至少两张'
        },
        {
            text:'门牌照片',
            name: 'doorplatePics',
            value: [],
            hink: '至少两张'
        },
        {
            text:'半开门照片',
            name: 'doubleDoorPics',
            value: [],
            picType: 13,
            hink: '至少两张'
        },
        {
            text:'房产证照片',
            name: 'houseCardPics',
            value: [],
            picType: 14,
            hink: '至少两张'
        },
        {
            text:'小区门口照片',
            name: 'doorPhotoPics',
            value: [],
            picType: 15,
            hink: '至少两张'
        }
    ];   
       
    uploadConf2.column2 = $scope.column2;
    uploadConf2.maxImgNum =  $scope.maxImgNum;
    uploadConf2.imgNew2 = $scope.imgNew2;

    //获取房源图片
    $scope.chooseModel = function(index,picModelList) {
        $scope.editPicFlag = true;

        $scope.imgNew2[0].value = [];
        $scope.imgNew2[1].value = [];
        $scope.imgNew2[3].value = [];
        $scope.imgNew2[4].value = [];
        $scope.imgNew2[5].value = [];

        var picModelId = picModelList[index].id;
        // console.log( picModelId );

        var param_3013 = {};
        param_3013.id = picModelId;
        $rootScope.modelId = picModelId;

        rest.post({
            methodId: '3013',
            data: param_3013,
            success: function(res) {
                if (res.code == 0) {
                    var data = res.res.data.content;
                    data = JSON.parse( data );
                    for (var i in data) {
                        if (data[i].PicType == 11) {
                            $scope.imgNew2[0].value.push(data[i].PicPath);
                        } else if (data[i].PicType == 12) {
                            $scope.imgNew2[1].value.push(data[i].PicPath);
                        } else if (data[i].PicType == 13) {
                            $scope.imgNew2[3].value.push(data[i].PicPath);
                        } else if (data[i].PicType == 14) {
                            $scope.imgNew2[4].value.push(data[i].PicPath);
                        } else if (data[i].PicType == 15) {
                            $scope.imgNew2[5].value.push(data[i].PicPath);
                        } 
                    }
                }

                $scope.$emit('contractPics', $scope.imgNew2[0]);
                $scope.$emit('roadPics', $scope.imgNew2[1]);
                $scope.$emit('doorplatePics', $scope.imgNew2[2]);
                $scope.$emit('doubleDoorPics', $scope.imgNew2[3]);
                $scope.$emit('houseCardPics', $scope.imgNew2[4]);
                $scope.$emit('doorPhotoPics', $scope.imgNew2[5]);

                $scope.$apply();
            },
            error: function() {
                $scope.disable_save_btn = false;
            }
        });
        $scope.$apply();
    }

    $scope.$watch('contractPics', function(newValue, oldValue) {
      $scope.$emit('contractPics', $scope.imgNew2[0]);
    })
    $scope.$watch('roadPics', function(newValue, oldValue) {
      $scope.$emit('roadPics', $scope.imgNew2[1]);
    })
    $scope.$watch('doorplatePics', function(newValue, oldValue) {
      $scope.$emit('doorplatePics', $scope.imgNew2[2]);
    })
    $scope.$watch('doubleDoorPics', function(newValue, oldValue) {
      $scope.$emit('doubleDoorPics', $scope.imgNew2[3]);
    })
    $scope.$watch('houseCardPics', function(newValue, oldValue) {
      $scope.$emit('houseCardPics', $scope.imgNew2[4]);
    })
    $scope.$watch('doorPhotoPics', function(newValue, oldValue) {
      $scope.$emit('doorPhotoPics', $scope.imgNew2[5]);
    })

});
